//
//  WelcomeViewController.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/15.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class WelcomeViewController: UIViewController {
    
    override func loadView() {
//        print(view)
        view = backImageView
//        print(view)
//        view.backgroundColor = UIColor.redColor()
        
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupUI()

        // Do any additional setup after loading the view.
    }
    
    
    //动画效果不推荐在viewdidload/loadView中执行动画
    //推荐在viewDidApear中执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        //执行动画
        showAnimation()
    }
    
    private func setupUI (){
    
        self.view.addSubview(headImageView)
        self.view.addSubview(welcomeLabel)
        
        //设置约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(headImageView.snp_centerX)
        }
        
        //头像圆角设置
        headImageView.layer.cornerRadius = 45
        headImageView.layer.masksToBounds = true
        
        
        //隐藏文案
        welcomeLabel.alpha = 0
        
        //显示用户头像
        headImageView.sd_setImageWithURL(UserAccountViewModel().headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
        
        
    
    }
    
    
    //MARK: 显示动画效果
    private func showAnimation(){
        //在动画闭包中修改 头像的地步约束
        //更新某一条已经添加过的约束 如果改约束不存在会自动添加给约束
        
        //usingSpringWithDamping:弹簧系数  0.1 ～ 1 越小 越弹
        //initialSpringVelocity 加速度
        //options: 动画可选项  oc按位枚举  swift： 数组
        
        //弹簧系数设置准则 （弹簧系数 ＊ 10  ～＝ 加速度）
        
        //使用自动布局 ＋ 动画闭包 ＝＝＝》 强制刷新视图
        
        let offset = -UIScreen.mainScreen().bounds.height + 160
        
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.bottom.equalTo(self.view.snp_bottom).offset(offset)
        }
        
//        UIView.animateWithDuration(1.5, delay: 0, options: <#T##UIViewAnimationOptions#>, animations: <#T##() -> Void#>, completion: <#T##((Bool) -> Void)?##((Bool) -> Void)?##(Bool) -> Void#>)
        
        UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.98, initialSpringVelocity: 9.8, options: [], animations: { () -> Void in
            
            //放在代码块中执行 并且需要在修该约束之后调用
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                //显示文案
                UIView.animateWithDuration(0.6, animations: { () -> Void in
                self.welcomeLabel.alpha = 1.0
                    }, completion: { (_) -> Void in
                        print("ok")
                        //发出显示首页的通知
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVC, object: nil)
                        
                })
                
                
        }
        //
        //
        
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //  懒加载子视图
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    // 用户头像
    private lazy var headImageView: UIImageView =   UIImageView(image: UIImage(named: "avatar_default_big"))
    
    
    private lazy var welcomeLabel: UILabel = {
    
        let l = UILabel()
    
        l.text = (UserAccountViewModel().userName ?? "") + " 欢迎归来"
        
//        print(UserAccountViewModel().userName)
        l.textColor = UIColor.purpleColor()
        l.font = UIFont.systemFontOfSize(16)
        
        //设置大小
        l.sizeToFit()
        
        return l
    }()

}
